package com.xiyuan.action;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.xiyuan.util.PropertyUtils;
import com.xiyuan.util.UploadFileException;
import com.xiyuan.util.UploadManagerImpl;

@Controller
public class UploadAction {

	private UploadManagerImpl uploadService = new UploadManagerImpl();
	
	@Autowired
	private  PropertyUtils propertyConfigurer;


	@RequestMapping(value = "/upload.jsp")
	@ResponseBody
	public void upload(HttpServletRequest request, PrintWriter out) {
		String filePath = "";
		if (request instanceof MultipartHttpServletRequest) {
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
			MultipartFile mfile1 = null;
			mfile1 = multipartRequest.getFile("Filedata");
			mfile1.getSize();
			String rootPath = propertyConfigurer.getValue("rootPath");
			try {
				uploadService.checkUploadImage(mfile1);
				uploadService.setRootPath(rootPath);
				filePath = uploadService.uploadFile(mfile1);
			} catch (UploadFileException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		out.println(filePath);
	}
	
	
	

	
	
}
